use actix_web::{
    web::{self, ServiceConfig},
    HttpResponse,
};

use crate::{
    errors::cons_error,
    services::user_service::{RegistryUserReq, UserService},
    AppError, AppResult, ArcConfig,
};

pub async fn registry_user(
    user_service: web::Data<UserService>,
    cfg: web::Data<ArcConfig>,
    req: web::Json<RegistryUserReq>,
) -> AppResult<HttpResponse> {
    if !cfg.app.allow_register {
        return Err(AppError::forbidden(cons_error::MSG_NOT_ALLOW_REGISTER));
    }
    let req_inner = req.into_inner();
    user_service.registry_user(&req_inner).await?;
    Ok(HttpResponse::NoContent().finish())
}

pub fn configure_registry_controller(config: &mut ServiceConfig) {
    config.service(web::resource("/ap").route(web::post().to(registry_user)));
}
